<template>
  <div class="screen-root">
    <!-- 算法配置面板 -->
    <AlgorithmConfig 
      v-model:show-panel="showConfigPanel"
      @config-change="handleConfigChange"
    />
    <!-- 红色预警遮罩 -->
    <div v-if="isAlerting" class="alert-overlay">
      <div class="alert-content">
        <button class="alert-close" @click="closeAlert" title="关闭预警">×</button>
        <div class="alert-header">
          <h2 class="alert-title">{{ currentAlert?.type === 'fire' ? '🔥 火灾预警' : '☢️ 放射源预警' }}</h2>
          <div class="alert-time">
            <span class="label">预警时间：</span>
            <span class="value">{{ alertTime }}</span>
          </div>
          <div class="alert-location">
            <span class="label">报警位置：</span>
            <span class="value">{{ currentAlert?.location }}</span>
          </div>
          <div v-if="currentAlert?.type === 'radiation'" class="alert-responsible">
            <span class="label">当前责任人：</span>
            <span class="value">{{ currentAlert?.responsible }}</span>
          </div>
        </div>
        <div class="alert-video">
          <div class="video-placeholder">
            <div class="video-frame">
              <div class="video-content">
                <span class="video-icon">📹</span>
                <p class="video-text">监控画面</p>
                <p class="video-label">{{ currentAlert?.camera }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="dashboard">
      <TopBar ref="topBarRef" @alert="handleAlert" @open-config="showConfigPanel = true" />
      <!-- 标准模式 -->
      <main v-if="viewMode === 'standard'" class="main-grid" :class="{ collapsed: isCollapsed }">
        <aside class="side-left">
          <LeftPanel />
        </aside>
        <section class="center-area">
          <div class="scene-block">
            <ThreeScene :collapsed="isCollapsed" @toggle-collapse="toggleCollapse" />
            <!-- <div class="scene-floating">
              <div class="floating-card" v-for="item in floatingCards" :key="item.label">
                <h3>{{ item.value }}</h3>
                <p>{{ item.label }}</p>
                <span>{{ item.desc }}</span>
              </div>
            </div> -->
          </div>
          <div class="center-bottom">
            <BottomPanel />
          </div>
        </section>
        <aside class="side-right">
          <RightPanel />
        </aside>
      </main>

      <!-- 简洁模式 -->
      <div v-else class="simple-mode-wrapper">
        <SimpleMode />
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, computed } from 'vue'
import { useDateFormat } from '@vueuse/core'
import TopBar from './components/TopBar.vue'
import LeftPanel from './components/LeftPanel.vue'
import RightPanel from './components/RightPanel.vue'
import BottomPanel from './components/BottomPanel.vue'
import ThreeScene from './components/ThreeScene.vue'
import SimpleMode from './components/SimpleMode.vue'
import AlgorithmConfig from './components/AlgorithmConfig.vue'

const topBarRef = ref(null)
const isAlerting = ref(false)
const currentAlert = ref(null)
const alertTriggerTime = ref(null)
const showConfigPanel = ref(false)
const isCollapsed = ref(false) // 收缩状态
let alertTimeout = null

const floatingCards = reactive([
  { label: 'B04车间', value: '54人', desc: '作业中 · 2处重点监看' },
  { label: 'C07车间', value: '38人', desc: '班组交接 · 1处隐患复核' },
  { label: '堆放场', value: '12人', desc: '物料入库 · 执行防火检查' }
])

const viewMode = computed(() => topBarRef.value?.viewMode || 'standard')

// 格式化预警时间
const alertTime = computed(() => {
  if (!alertTriggerTime.value) return ''
  const formatted = useDateFormat(alertTriggerTime.value, 'YYYY-MM-DD HH:mm:ss')
  // useDateFormat返回一个Ref或字符串
  return formatted.value || formatted
})

// 模拟报警位置和摄像头数据
const alertLocations = {
  fire: [
    { location: 'B04车间 - 东侧区域', camera: '摄像头 #B04-03' },
    { location: 'C07车间 - 西南角', camera: '摄像头 #C07-05' },
    { location: '堆放场 - 北区', camera: '摄像头 #ST-02' }
  ],
  radiation: [
    { location: 'B04车间 - 放射源存放区', camera: '摄像头 #B04-08', responsible: '张明' },
    { location: 'C07车间 - 检测室', camera: '摄像头 #C07-12', responsible: '李华' },
    { location: '堆放场 - 危险品区', camera: '摄像头 #ST-09', responsible: '王强' }
  ]
}

const handleAlert = (alertInfo) => {
  // 清除之前的定时器
  if (alertTimeout) {
    clearTimeout(alertTimeout)
  }
  
  // 记录预警触发时间
  alertTriggerTime.value = new Date()
  
  // 随机选择一个报警位置
  const locations = alertLocations[alertInfo.type] || []
  const randomLocation = locations[Math.floor(Math.random() * locations.length)] || {
    location: '未知位置',
    camera: '摄像头 #N/A',
    responsible: '待确认'
  }
  
  // 设置当前报警信息
  currentAlert.value = {
    type: alertInfo.type,
    location: randomLocation.location,
    camera: randomLocation.camera,
    responsible: randomLocation.responsible || '待确认'
  }
  
  // 触发预警
  isAlerting.value = true
  
  // 3秒后停止闪烁（可以根据需要调整时间）
  alertTimeout = setTimeout(() => {
    closeAlert()
  }, 3000)
}

const closeAlert = () => {
  // 清除定时器
  if (alertTimeout) {
    clearTimeout(alertTimeout)
    alertTimeout = null
  }
  
  // 关闭预警
  isAlerting.value = false
  currentAlert.value = null
  alertTriggerTime.value = null
}

const handleConfigChange = (config) => {
  // 处理配置变更，可以在这里发送到后端或进行其他操作
  console.log('算法配置已更新:', config)
}

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

</script>

<style scoped>
.screen-root {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: radial-gradient(circle at center, rgba(14, 42, 80, 0.8), rgba(2, 14, 28, 0.95));
}

.dashboard {
  display: grid;
  grid-template-rows: 70px 1fr;
  gap: 20px;
  padding: 20px;
  box-sizing: border-box;
  height: calc(100vh - 40px);
  width: 100%;
}

.main-grid {
  display: grid;
  grid-template-columns: 300px 1fr 300px;
  grid-template-rows: calc(100vh - 130px);
  gap: 20px;
  transition: grid-template-columns 0.4s cubic-bezier(0.4, 0, 0.2, 1), gap 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.main-grid.collapsed {
  grid-template-columns: 0 1fr 0;
  gap: 0;
}

.side-left,
.side-right {
  min-height: 0;
  overflow: hidden;
  transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1), transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.main-grid.collapsed .side-left {
  opacity: 0;
  transform: translateX(-100%);
  pointer-events: none;
}

.main-grid.collapsed .side-right {
  opacity: 0;
  transform: translateX(100%);
  pointer-events: none;
}

.center-area {
  display: grid;
  grid-template-rows: 1fr 200px;
  gap: 20px;
  height: 100%;
  transition: grid-template-rows 0.4s cubic-bezier(0.4, 0, 0.2, 1), gap 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.main-grid.collapsed .center-area {
  grid-template-rows: 1fr 0;
  gap: 0;
}

.center-bottom {
  overflow: hidden;
  transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1), transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.main-grid.collapsed .center-bottom {
  opacity: 0;
  transform: translateY(100%);
  pointer-events: none;
}

.simple-mode-wrapper {
  height: calc(100vh - 130px); /* 减去TopBar高度(70px) + padding(20px*2) + gap(20px) */
  width: 100%;
}

.alert-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
  pointer-events: none;
  overflow: hidden;
}

/* 多层渐变遮罩，实现更柔和的效果 */
.alert-overlay::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* 主渐变：中心完全透明，边缘柔和过渡 */
  background: radial-gradient(
    ellipse 80% 60% at center,
    rgba(255, 50, 50, 0) 0%,
    rgba(255, 50, 50, 0.05) 30%,
    rgba(255, 30, 30, 0.15) 50%,
    rgba(200, 0, 0, 0.25) 70%,
    rgba(150, 0, 0, 0.35) 85%,
    rgba(100, 0, 0, 0.45) 100%
  );
  animation: alertPulse 2s ease-in-out infinite;
}

/* 第二层：边缘加深 */
.alert-overlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: 
    linear-gradient(180deg, rgba(150, 0, 0, 0.2) 0%, transparent 20%, transparent 80%, rgba(150, 0, 0, 0.2) 100%),
    linear-gradient(90deg, rgba(150, 0, 0, 0.2) 0%, transparent 25%, transparent 75%, rgba(150, 0, 0, 0.2) 100%);
  animation: alertPulse 2.5s ease-in-out infinite reverse;
  opacity: 0.6;
}

@keyframes alertPulse {
  0%, 100% {
    opacity: 0.7;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.02);
  }
}

.alert-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 800px;
  max-width: 90vw;
  background: linear-gradient(
    135deg,
    rgba(15, 25, 40, 0.98) 0%,
    rgba(10, 18, 32, 0.98) 100%
  );
  border: 1px solid rgba(255, 80, 80, 0.4);
  border-radius: 20px;
  padding: 32px;
  box-shadow: 
    0 8px 48px rgba(0, 0, 0, 0.6),
    0 0 0 1px rgba(255, 100, 100, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    0 0 80px rgba(255, 50, 50, 0.4);
  pointer-events: auto;
  backdrop-filter: blur(10px);
  animation: alertContentFloat 3s ease-in-out infinite;
  position: relative;
  overflow: hidden;
}

.alert-content::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 100, 100, 0.8),
    rgba(255, 50, 50, 1),
    rgba(255, 100, 100, 0.8),
    transparent
  );
  animation: alertShimmer 2s linear infinite;
}

@keyframes alertShimmer {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

@keyframes alertContentFloat {
  0%, 100% {
    transform: translate(-50%, -50%) scale(1);
    box-shadow: 
      0 8px 48px rgba(0, 0, 0, 0.6),
      0 0 0 1px rgba(255, 100, 100, 0.3),
      inset 0 1px 0 rgba(255, 255, 255, 0.1),
      0 0 80px rgba(255, 50, 50, 0.4);
  }
  50% {
    transform: translate(-50%, -50%) scale(1.01);
    box-shadow: 
      0 12px 60px rgba(0, 0, 0, 0.7),
      0 0 0 1px rgba(255, 120, 120, 0.5),
      inset 0 1px 0 rgba(255, 255, 255, 0.15),
      0 0 120px rgba(255, 70, 70, 0.6);
  }
}

.alert-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 100, 100, 0.2);
  color: #ffcccc;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  z-index: 10;
}

.alert-close:hover {
  background: rgba(255, 80, 80, 0.4);
  color: #ffffff;
  transform: rotate(90deg) scale(1.1);
  box-shadow: 0 0 16px rgba(255, 100, 100, 0.6);
}

.alert-close:active {
  transform: rotate(90deg) scale(0.95);
}

.alert-header {
  margin-bottom: 20px;
}

.alert-title {
  margin: 0 0 20px 0;
  font-size: 32px;
  font-weight: 700;
  background: linear-gradient(135deg, #ff5555 0%, #ff3333 50%, #ff8888 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-align: center;
  letter-spacing: 1px;
  position: relative;
  padding-bottom: 12px;
}

.alert-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100px;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(255, 100, 100, 0.8), transparent);
}

.alert-time,
.alert-location,
.alert-responsible {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  font-size: 16px;
}

.alert-time .label,
.alert-location .label,
.alert-responsible .label {
  color: rgba(255, 200, 200, 0.9);
  font-weight: 500;
}

.alert-time .value,
.alert-location .value,
.alert-responsible .value {
  color: #ffcc00;
  font-weight: 600;
  text-shadow: 0 0 10px rgba(255, 204, 0, 0.6);
}

.alert-video {
  margin-top: 20px;
}

.video-placeholder {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: 
    radial-gradient(circle at 30% 30%, rgba(255, 50, 50, 0.1), transparent 50%),
    linear-gradient(135deg, rgba(20, 30, 50, 0.95), rgba(10, 15, 30, 0.95));
  border: 1px solid rgba(255, 100, 100, 0.3);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  box-shadow: 
    inset 0 2px 8px rgba(0, 0, 0, 0.3),
    0 0 20px rgba(255, 50, 50, 0.2);
}

.video-placeholder::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 150, 150, 0.08),
    rgba(255, 200, 200, 0.12),
    rgba(255, 150, 150, 0.08),
    transparent
  );
  animation: videoScan 3s ease-in-out infinite;
}

@keyframes videoScan {
  0% {
    left: -100%;
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    left: 100%;
    opacity: 0;
  }
}

.video-frame {
  position: relative;
  z-index: 1;
  text-align: center;
}

.video-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.video-icon {
  font-size: 64px;
  opacity: 0.6;
}

.video-text {
  margin: 0;
  font-size: 20px;
  color: rgba(255, 200, 200, 0.8);
  font-weight: 500;
}

.video-label {
  margin: 0;
  font-size: 14px;
  color: rgba(255, 150, 150, 0.7);
  font-weight: 400;
}

.scene-block {
  position: relative;
  height: 100%;
  border-radius: 18px;
  overflow: hidden;
  border: 1px solid rgba(24, 118, 200, 0.35);
  background: rgba(5, 26, 48, 0.85);
}

.center-bottom {
  border-radius: 18px;
  border: 1px solid rgba(24, 118, 200, 0.35);
  background: rgba(5, 26, 48, 0.85);
  overflow: hidden;
}

.scene-floating {
  position: absolute;
  top: 18px;
  left: 18px;
  display: grid;
  gap: 10px;
}

.floating-card {
  min-width: 150px;
  padding: 9px 12px;
  border-radius: 10px;
  background: rgba(12, 65, 112, 0.78);
  border: 1px solid rgba(41, 171, 255, 0.45);
  color: #d4efff;
}

.floating-card h3 {
  margin: 0;
  font-size: 14px;
  color: #4ce6ff;
}

.floating-card p {
  margin: 4px 0;
  font-size: 11px;
}

.floating-card span {
  font-size: 10px;
  color: rgba(163, 214, 255, 0.78);
}
</style>





